using System;
using System.Collections.Generic;
using System.Text;

namespace Ndo
{
    public class InheritanceException : Exception
    {
        public InheritanceException(Type inheritedType)
        {
            this.inheritedType = inheritedType;
        }

        private Type inheritedType;
        public Type InheritedType { get { return inheritedType; } }
    }
    public class Object<TFinal> : IImplement<TFinal>
        where TFinal : Object<TFinal>
    {
        /// <summary>
        /// 0: unchecked, 1: valid, -1: invalid
        /// </summary>
        private static int flag = 0;
        protected Object()
        {
            switch (flag)
            {
                case 0:
                    if (this.GetType() != typeof (TFinal))
                    {
                        flag = -1;
                        throw new InheritanceException(typeof (TFinal));
                    }
                    flag = 1;
                    break;
                case -1:
                    throw new InheritanceException(typeof(TFinal));
            }
        }

        protected TFinal Self
        {
            get { return (TFinal) this; }
        }
    }
}
